{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1.url\n",
    "base_url='https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "#2.api账户/通行证\n",
    "API_key=\"U-d6Okv9dUcg-ZQVJhxz7Ztgxvzn9YJJ\"    #直接复制容易产生-->使得结果401,记得删掉\n",
    "API_sercret=\"KUd9SBw4KX7AzVaFCZ4ngqB4r4-AJND8\"\n",
    "\n",
    "#3.POST\n",
    "\n",
    "#4.所有API key都可以调用本API\n",
    "# 其中calculate_all和face_rectangle参数只有正式API key才能使用,试用API Key无法使用\n",
    "\n",
    "#5.payload :酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload={\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n",
    "r=requests.post(base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648548113,61f7ccd8-1896-412e-89f5-1a062b85c3f1',\n",
       " 'time_used': 434,\n",
       " 'faces': [{'face_token': '7ca09417d1c4cca036a4fa4b471471ff',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸分析(尝试)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url='https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "API_key=\"U-d6Okv9dUcg-ZQVJhxz7Ztgxvzn9YJJ\"    \n",
    "API_sercret=\"KUd9SBw4KX7AzVaFCZ4ngqB4r4-AJND8\"\n",
    "Face_tokens='aabbc1194b00d9af8117cb355dbf9f3a'\n",
    "\n",
    "payload={\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':Face_tokens,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "r=requests.post(base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 76,\n",
       " 'error_message': 'INVALID_FACE_TOKEN: aabbc1194b00d9af8117cb355dbf9f3a',\n",
       " 'request_id': '1648548121,a634645e-9377-40ee-a9b2-df0f652c1763'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#人脸识别\n",
    "base_url='https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "API_key=\"U-d6Okv9dUcg-ZQVJhxz7Ztgxvzn9YJJ\"  \n",
    "API_sercret=\"KUd9SBw4KX7AzVaFCZ4ngqB4r4-AJND8\"\n",
    "payload={\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse4-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?w=117&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7'\n",
    "}\n",
    "r=requests.post(base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648548131,5dd46071-da59-4434-b315-72830bc30173',\n",
       " 'time_used': 408,\n",
       " 'faces': [{'face_token': 'db2f38532982503b14ee8c6503163d60',\n",
       "   'face_rectangle': {'top': 86, 'left': 35, 'width': 89, 'height': 89}}],\n",
       " 'image_id': '/nX85FuxHrVV67jAVFVKCA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'face_token': 'db2f38532982503b14ee8c6503163d60',\n",
       "  'face_rectangle': {'top': 86, 'left': 35, 'width': 89, 'height': 89}}]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>db2f38532982503b14ee8c6503163d60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                      0\n",
       "face_token             db2f38532982503b14ee8c6503163d60\n",
       "face_rectangle.top                                   86\n",
       "face_rectangle.left                                  35\n",
       "face_rectangle.width                                 89\n",
       "face_rectangle.height                                89"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces'][0]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "Face_tokens1='aabbc1194b00d9af8117cb355dbf9f3a'\n",
    "Face_tokens2='b8a97d2bd24544772f40a2679f4ccb88'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url='https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "API_key=\"U-d6Okv9dUcg-ZQVJhxz7Ztgxvzn9YJJ\"    \n",
    "API_sercret=\"KUd9SBw4KX7AzVaFCZ4ngqB4r4-AJND8\"\n",
    "payload={\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':Face_tokens1,\n",
    "    'face_token2':Face_tokens2\n",
    " \n",
    "}\n",
    "r=requests.post(base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648548141,44276f0f-b56c-4da6-a143-1b85b9c8f448',\n",
       " 'time_used': 49,\n",
       " 'error_message': \"INVALID_FACE_TOKEN: ['aabbc1194b00d9af8117cb355dbf9f3a']\"}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸检测函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key=\"U-d6Okv9dUcg-ZQVJhxz7Ztgxvzn9YJJ\"    \n",
    "API_secret=\"KUd9SBw4KX7AzVaFCZ4ngqB4r4-AJND8\"\n",
    "image_url='https://tse4-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?w=117&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考:能否将一些功能作函数的封装,实现更加便捷的API/调用\n",
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    url='https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max\n",
    "        \n",
    "    }\n",
    "    r=requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648548159,61e557ef-2d50-4c29-bc2c-48aa5f18b1a6',\n",
       " 'time_used': 312,\n",
       " 'faces': [{'face_token': '131326030fc3b7c2700386b0737d7a4f',\n",
       "   'face_rectangle': {'top': 86, 'left': 35, 'width': 89, 'height': 89}}],\n",
       " 'image_id': '/nX85FuxHrVV67jAVFVKCA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用函数\n",
    "face_detect(API_key,API_secret,image_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建人脸库API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url='https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "API_key=\"U-d6Okv9dUcg-ZQVJhxz7Ztgxvzn9YJJ\"    \n",
    "API_sercret=\"KUd9SBw4KX7AzVaFCZ4ngqB4r4-AJND8\"\n",
    "display_nameS='PLX'\n",
    "Face_tokens='aabbc1194b00d9af8117cb355dbf9f3a'\n",
    "\n",
    "payload={\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':Face_tokens,\n",
    "    'display_name':display_nameS,\n",
    "    'face_tokens':Face_tokens\n",
    "}\n",
    "r=requests.post(base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '8eb752e83f64473c36353b58d3af5b96',\n",
       " 'time_used': 180,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648548163,187d6a27-9699-45a2-a443-cd1cd4608a9b',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': 'aabbc1194b00d9af8117cb355dbf9f3a'}]}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "303.837px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
